using System;
using System.Collections;
using UnityEngine;
using XLua.LuaDLL;

namespace XLua
{
	public class DelegateBridge : DelegateBridgeBase
	{
		internal static DelegateBridge[] DelegateBridgeList;

		public static bool Gen_Flag;

		public int __Gen_Delegate_Imp0(int p0, string p1, out CSCallLua.DClass p2)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			Lua.xlua_pushinteger(rawL, p0);
			Lua.lua_pushstring(rawL, p1);
			PCall(rawL, 2, 2, num);
			p2 = (CSCallLua.DClass)translator.GetObject(rawL, num + 2, typeof(CSCallLua.DClass));
			int result = Lua.xlua_tointeger(rawL, num + 1);
			Lua.lua_settop(rawL, num - 1);
			return result;
		}

		public Action __Gen_Delegate_Imp1()
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			PCall(rawL, 0, 1, num);
			Action @delegate = translator.GetDelegate<Action>(rawL, num + 1);
			Lua.lua_settop(rawL, num - 1);
			return @delegate;
		}

		public CastleSceneUIManager __Gen_Delegate_Imp2()
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			PCall(rawL, 0, 1, num);
			CastleSceneUIManager result = (CastleSceneUIManager)translator.GetObject(rawL, num + 1, typeof(CastleSceneUIManager));
			Lua.lua_settop(rawL, num - 1);
			return result;
		}

		public void __Gen_Delegate_Imp3(object p0)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			luaEnv.translator.PushAny(rawL, p0);
			PCall(rawL, 1, 0, num);
			Lua.lua_settop(rawL, num - 1);
		}

		public void __Gen_Delegate_Imp4(object p0, uint p1, object p2)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			Lua.xlua_pushuint(rawL, p1);
			translator.PushAny(rawL, p2);
			PCall(rawL, 3, 0, num);
			Lua.lua_settop(rawL, num - 1);
		}

		public void __Gen_Delegate_Imp5(object p0, object p1, object p2)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			translator.PushAny(rawL, p1);
			translator.PushAny(rawL, p2);
			PCall(rawL, 3, 0, num);
			Lua.lua_settop(rawL, num - 1);
		}

		public bool __Gen_Delegate_Imp6(object p0)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			luaEnv.translator.PushAny(rawL, p0);
			PCall(rawL, 1, 1, num);
			bool result = Lua.lua_toboolean(rawL, num + 1);
			Lua.lua_settop(rawL, num - 1);
			return result;
		}

		public Vector3 __Gen_Delegate_Imp7(object p0, object p1)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			translator.PushAny(rawL, p1);
			PCall(rawL, 2, 1, num);
			translator.Get(rawL, num + 1, out Vector3 val);
			Lua.lua_settop(rawL, num - 1);
			return val;
		}

		public void __Gen_Delegate_Imp8(object p0, object p1)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			translator.PushAny(rawL, p1);
			PCall(rawL, 2, 0, num);
			Lua.lua_settop(rawL, num - 1);
		}

		public void __Gen_Delegate_Imp9(object p0, int p1, Vector3 p2, int p3)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			Lua.xlua_pushinteger(rawL, p1);
			translator.PushUnityEngineVector3(rawL, p2);
			Lua.xlua_pushinteger(rawL, p3);
			PCall(rawL, 4, 0, num);
			Lua.lua_settop(rawL, num - 1);
		}

		public IEnumerator __Gen_Delegate_Imp10(object p0, int p1, Vector3 p2, int p3)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			Lua.xlua_pushinteger(rawL, p1);
			translator.PushUnityEngineVector3(rawL, p2);
			Lua.xlua_pushinteger(rawL, p3);
			PCall(rawL, 4, 1, num);
			IEnumerator result = (IEnumerator)translator.GetObject(rawL, num + 1, typeof(IEnumerator));
			Lua.lua_settop(rawL, num - 1);
			return result;
		}

		public void __Gen_Delegate_Imp11(object p0, object p1, bool p2)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			translator.PushAny(rawL, p1);
			Lua.lua_pushboolean(rawL, p2);
			PCall(rawL, 3, 0, num);
			Lua.lua_settop(rawL, num - 1);
		}

		public IEnumerator __Gen_Delegate_Imp12(object p0, object p1)
		{
			IntPtr rawL = luaEnv.rawL;
			int num = Lua.pcall_prepare(rawL, errorFuncRef, luaReference);
			ObjectTranslator translator = luaEnv.translator;
			translator.PushAny(rawL, p0);
			translator.PushAny(rawL, p1);
			PCall(rawL, 2, 1, num);
			IEnumerator result = (IEnumerator)translator.GetObject(rawL, num + 1, typeof(IEnumerator));
			Lua.lua_settop(rawL, num - 1);
			return result;
		}

		static DelegateBridge()
		{
			DelegateBridgeList = new DelegateBridge[0];
			Gen_Flag = false;
			Gen_Flag = true;
		}

		public override Delegate GetDelegateByType(Type type)
		{
			if (type == typeof(CSCallLua.FDelegate))
			{
				return new CSCallLua.FDelegate(this.__Gen_Delegate_Imp0);
			}
			if (type == typeof(CSCallLua.GetE))
			{
				return new CSCallLua.GetE(__Gen_Delegate_Imp1);
			}
			return null;
		}

		public DelegateBridge(int reference, LuaEnv luaenv)
			: base(reference, luaenv)
		{
		}

		public void PCall(IntPtr L, int nArgs, int nResults, int errFunc)
		{
			if (Lua.lua_pcall(L, nArgs, nResults, errFunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(errFunc - 1);
			}
		}

		public void Action()
		{
			IntPtr l = luaEnv.L;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			if (Lua.lua_pcall(l, 0, 0, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			Lua.lua_settop(l, num);
		}

		public void Action<T1>(T1 p1)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			if (Lua.lua_pcall(l, 1, 0, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			Lua.lua_settop(l, num);
		}

		public void Action<T1, T2>(T1 p1, T2 p2)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			translator.PushByType(l, p2);
			if (Lua.lua_pcall(l, 2, 0, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			Lua.lua_settop(l, num);
		}

		public void Action<T1, T2, T3>(T1 p1, T2 p2, T3 p3)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			translator.PushByType(l, p2);
			translator.PushByType(l, p3);
			if (Lua.lua_pcall(l, 3, 0, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			Lua.lua_settop(l, num);
		}

		public void Action<T1, T2, T3, T4>(T1 p1, T2 p2, T3 p3, T4 p4)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			translator.PushByType(l, p2);
			translator.PushByType(l, p3);
			translator.PushByType(l, p4);
			if (Lua.lua_pcall(l, 4, 0, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			Lua.lua_settop(l, num);
		}

		public TResult Func<TResult>()
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			if (Lua.lua_pcall(l, 0, 1, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			try
			{
				translator.Get(l, -1, out TResult v);
				return v;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				Lua.lua_settop(l, num);
			}
		}

		public TResult Func<T1, TResult>(T1 p1)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			if (Lua.lua_pcall(l, 1, 1, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			try
			{
				translator.Get(l, -1, out TResult v);
				return v;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				Lua.lua_settop(l, num);
			}
		}

		public TResult Func<T1, T2, TResult>(T1 p1, T2 p2)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			translator.PushByType(l, p2);
			if (Lua.lua_pcall(l, 2, 1, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			try
			{
				translator.Get(l, -1, out TResult v);
				return v;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				Lua.lua_settop(l, num);
			}
		}

		public TResult Func<T1, T2, T3, TResult>(T1 p1, T2 p2, T3 p3)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			translator.PushByType(l, p2);
			translator.PushByType(l, p3);
			if (Lua.lua_pcall(l, 3, 1, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			try
			{
				translator.Get(l, -1, out TResult v);
				return v;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				Lua.lua_settop(l, num);
			}
		}

		public TResult Func<T1, T2, T3, T4, TResult>(T1 p1, T2 p2, T3 p3, T4 p4)
		{
			IntPtr l = luaEnv.L;
			ObjectTranslator translator = luaEnv.translator;
			int num = Lua.lua_gettop(l);
			int errfunc = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			translator.PushByType(l, p1);
			translator.PushByType(l, p2);
			translator.PushByType(l, p3);
			translator.PushByType(l, p4);
			if (Lua.lua_pcall(l, 4, 1, errfunc) != 0)
			{
				luaEnv.ThrowExceptionFromError(num);
			}
			try
			{
				translator.Get(l, -1, out TResult v);
				return v;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				Lua.lua_settop(l, num);
			}
		}
	}
}
